package training_camp.day2.kama44;

import java.util.Arrays;
import java.util.Scanner;

/**
 * description :
 * author Ahang :
 * create : 2025-11-20 14:42
 */
public class Main {

    public static void main(String[] args) {
        int n, m;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        int sum = 0;
        int[][] graph = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                graph[i][j] = scanner.nextInt();
                sum += graph[i][j];
            }
        }
        int result = Integer.MAX_VALUE;
        int window = 0;
//      遍历行
        int[] col = new int[m];
        int[] cow = new int[n];
        Arrays.fill(cow, 0);
        Arrays.fill(col, 0);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                col[j] = col[j] + graph[i][j];
                cow[i] = cow[i] + graph[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            window += cow[i];
            result = Math.min(Math.abs(sum - 2 * window), result);
        }
        window = 0;
        for (int i = 0; i < m; i++) {
            window += col[i];
            result = Math.min(Math.abs(sum - 2 * window), result);
        }
        System.out.println(result);
    }


}
